/*	$Id: conf.h 328 2007-05-17 00:15:33Z phrakt $	*/
/*
 * Copyright (c) 2004 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _CANOPY_CONF_H_
#define _CANOPY_CONF_H_


#define CONF_LINE_MAXLEN	1024
#define CONF_BUF_DEFSIZE	4096
#define CONF_BUF_MAXSIZE	16384
#define CONF_TOK_MAXLEN		1024

#define CONF_DIR_MAXARG		16

/* flags */
#define CONF_STRICTVAR    0x00000001	/* strict variable declaration */
#define CONF_STRICTDIR    0x00000002	/* strict directive checking */
#define CONF_STRICTTYPE   0x00000004	/* enforce strict variable typing */
#define CONF_VARSUBST     0x00000008	/* enable variable substitution */
#define CONF_MULTILINE    0x00000010	/* allow multiline statements */
#define CONF_PRAGMA       0x00000020	/* enable pragmas */
#define CONF_DEBUG        0x00000040	/* enable debugging to stderr */
#define CONF_STRICT      (CONF_STRICTVAR | CONF_STRICTDIR | CONF_STRICTTYPE)


/* parser control operations */
#define CONF_CTL_BUFSZ     1

enum conf_var_type {
	CONF_VAR_NONE,			/* no type */
	CONF_VAR_BOOL,			/* boolean */
	CONF_VAR_INT,			/* 64-bit integer */
	CONF_VAR_UINT,			/* unsigned 64-bit integer */
	CONF_VAR_STR,			/* character string */
	CONF_VAR_BINSTR,		/* arbitrary bit string */
	CONF_VAR_STRUCT,		/* complex structure */
};

#define CONF_VAR_MAXTYPE   CONF_VAR_STRUCT 	/* keep in sync */


/* error codes */
#define CONF_ERR_NOERR     0
#define CONF_ERR_BADNAME   1	/* invalid name */
#define CONF_ERR_STRICT    2	/* strict rule broken */
#define CONF_ERR_BADTOK    3	/* invalid token */
#define CONF_ERR_NOMEM     4	/* out of memory */
#define CONF_ERR_INVARG    5	/* invalid argument */
#define CONF_ERR_PARSE     6	/* unrecoverable parse error */
#define CONF_ERR_NOTSUP    7	/* operation not supported */
#define CONF_ERR_TOKLEN    8	/* token too long */
#define CONF_ERR_UNTQSTR   9	/* unterminated quoted string */
#define CONF_ERR_ARGC     10	/* argument count too large */



typedef struct cnp_conf CONF;


CONF*  conf_alloc   (int);
void   conf_free    (CONF *);
void   conf_reset   (CONF *);
int    conf_setfl   (CONF *, int);
int    conf_getfl   (CONF *);
int    conf_parse   (CONF *, const char *);
void   conf_clrerr  (CONF *);
int    conf_geterr  (CONF *);
int    conf_setvar  (CONF *, const char *, int, const void *, size_t);
int    conf_getvar  (CONF *, const char *, void *, size_t);
int    conf_chkvar  (CONF *, const char *, int (*)(void *, size_t));
int    conf_setdir  (CONF *, const char *, int (*)(int, char **), int, int);


#define      conf_declvar(p, n, t) conf_parser_setvar(p, n, t, NULL)

#endif /* _CANOPY_CONF_H_ */
